using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Threading;
using SpoonCore2;
using SpoonCore2.Tools;

namespace SpoonCore2.Simulation
{
	class StartSimulation : ISimulation
	{
		private const int _serverPort = 1000;
		private readonly List<SpoonClient> _clientList = new List<SpoonClient>();

		public void Run()
		{
			SpoonClient server = new SpoonClient(_serverPort);
			server.Start();
			_clientList.Add(server);
			Thread.Sleep(250);

			int count = 100;
			while (count > 0)
			{
				SpoonClient client = new SpoonClient();
				client.Network.AddBootstrap(new IPEndPoint(IPAddress.Loopback, _serverPort));
				client.Start();

				Monitor.Enter(this);
				_clientList.Add(client);
				Monitor.Exit(this);

				Thread.Sleep(10);
				ColorConsole.WriteLine(client.Network.ClientInfo.Id);
				count--;
			}

			ColorConsole.WriteLine("Press enter to abort...", ConsoleColor.Yellow);
			ColorConsole.ReadLine();
			
			foreach (SpoonClient client in _clientList)
			{
				client.Dispose();
			}
		}

		public void Pause()
		{
			Monitor.Enter(this);
		}

		public void Resume()
		{
			Monitor.Exit(this);
		}

		public List<SpoonClient> Clients { get { return _clientList; } }
	}
}